#include <fstream>
#include <string>
#include <math.h>
#include <iostream>
#include "Light.h"

namespace glow
{
  using namespace std;
  
  Light::Light(GLenum NumLight): NumLight(NumLight) 
  {
    Light_Ambient[0] = 1.0;
    Light_Ambient[1] = 1.0;
    Light_Ambient[2] = 1.0;
    Light_Ambient[3] = 1.0;

    Light_Diffuse[0] = 1.0;
    Light_Diffuse[1] = 1.0;
    Light_Diffuse[2] = 1.0;
    Light_Diffuse[3] = 1.0;

    Light_Specular[0] = 1.0;
    Light_Specular[1] = 1.0;
    Light_Specular[2] = 1.0;
    Light_Specular[3] = 1.0;

    Light_Position[0] = 0.0;
    Light_Position[1] = 0.0;
    Light_Position[2] = 0.0;
    Light_Position[3] = 1.0;
  }
  
    void Light::on() const 
    {
    glEnable(GL_LIGHTING);    
    glEnable(NumLight);

    glLightfv( NumLight, GL_AMBIENT, Light_Ambient );
    glLightfv( NumLight, GL_DIFFUSE, Light_Diffuse );
    glLightfv( NumLight, GL_SPECULAR, Light_Specular );
    glLightfv( NumLight, GL_POSITION, Light_Position );
  }

  void Light::off() const 
  {  
    glDisable(GL_LIGHTING); 
    glDisable(NumLight);
  }
 
  void Light::setAmbient(const Color4f &color)
  {
    Light_Ambient[0] = color.r;
    Light_Ambient[1] = color.g;
    Light_Ambient[2] = color.b;
    Light_Ambient[3] = color.a;
  }

  void Light::setDiffuse(const Color4f &color)
{
    Light_Diffuse[0] = color.r;
    Light_Diffuse[1] = color.g;
    Light_Diffuse[2] = color.b;
    Light_Diffuse[3] = color.a;
  }

  void Light::setSpecular(const Color4f &color)
{
    Light_Specular[0] = color.r;
    Light_Specular[1] = color.g;
    Light_Specular[2] = color.b;
    Light_Specular[3] = color.a;
  }  

  void Light::setPosition(const Vector3f &position, int infinit)
{
    Light_Position[0] = position.x;
    Light_Position[1] = position.y;
    Light_Position[2] = position.z;
    Light_Position[3] = infinit;
  } 

  void Light::draw() const
  {
    glPushMatrix();
    glColor3ub(255, 255, 0);
    GLUquadric *params = gluNewQuadric();
    gluQuadricDrawStyle(params, GLU_FILL);
    glTranslatef(Light_Position[0], Light_Position[1], Light_Position[2]);
    gluSphere(params, 5., 16, 16);
    gluDeleteQuadric(params);
    glColor3ub(255, 255, 255);
    glPopMatrix();
  }

}
